package com.ymatou.iapi.settlement.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.settlement.parameter.OrderSettlementReq;
import com.ymatou.iapi.settlement.parameter.SettleSubsidyBean;
import com.ymatou.iapi.settlement.service.GenerateInvoicesOnOrderReceivedCall;
import com.ymatou.iapi.settlement.service.SettleSubsidyCall;
import com.ymatou.iapi.trading.operate.parameter.ActivityVo;
import com.ymatou.iapi.trading.operate.parameter.OrderGroupVo;
import com.ymatou.iapi.trading.operate.parameter.OrderItemVo;
import com.ymatou.iapi.trading.operate.parameter.OrderVo;
import com.ymatou.iapi.trading.operate.parameter.enums.ActivityPromotionTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.AppTerminalSourceEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.OrderSourceEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.OrderTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.PriceTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.ProductRefundChannelEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.SalesTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.enums.TerminalSourceEnum;
import com.ymatou.iapi.trading.operate.parameter.req.PlaceOrderReq;
import com.ymatou.iapi.trading.operate.service.PlaceOrderCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/**
 * 补贴结算接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SettleSubsidy {
	private static SettleSubsidyBean settleSubsidyBean;
	private static SettleSubsidyCall settleSubsidyCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("结算收入和扣佣接口");
	}

	@Before
	public void caseUp() {
		settleSubsidyBean = new SettleSubsidyBean();
		settleSubsidyCall = new SettleSubsidyCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(settleSubsidyCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/**
	 * 下单使用补贴商品
	 */
	private Map placeOrder2() {
		Map map = new HashMap();
		PlaceOrderReq placeorderBean = new PlaceOrderReq();
		PlaceOrderCall placeorderCall = new PlaceOrderCall();
		try {
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			// 创建产品
			Logger.comment("前置数据准备");
			// 获取补贴商品信息
			HashMap<String, Object> productInfo = new DefaultWapper()
					.selectActivityProductSubsidy();
			int sellerId = (Integer) productInfo.get("SubsidyUserId");
			String productId = (String) productInfo.get("ProductId");
			String catalogId = (String) productInfo.get("CatalogId");
			BigDecimal productPrice = (BigDecimal) productInfo
					.get("ProductPrice");
			int activityId = (Integer) productInfo.get("ActivityId");

			Logger.comment("前置数据准备结束");
			placeorderBean.setAppId("AutoTest");
			// placeorderBean.setYmatouCoupon(ymtCoupon);
			placeorderBean.setUserId(userId);
			placeorderBean.setRequestId(TestDataManager.getRandomUUID()
					.toString());
			placeorderBean.setTerminalSource(TerminalSourceEnum.IOS);
			placeorderBean.setAppTerminalSource(AppTerminalSourceEnum.Phone);
			placeorderBean.setSourceIp("127.0.0.1");
			placeorderBean.setAppId("ios");
			placeorderBean.setDeviceId("7777777777888888888888999");
			OrderGroupVo groupVo1 = new OrderGroupVo();
			placeorderBean.setOrderSource(OrderSourceEnum.PC);
			// groupVo1.setSellerCoupon(sellerCoupon);
			groupVo1.setSellerId(sellerId);
			List<OrderVo> orders = new ArrayList<OrderVo>();
			OrderVo ordervo1 = new OrderVo();
			ordervo1.setAutoCancelOrderHours(new BigDecimal(1));
			ordervo1.setOrderType(OrderTypeEnum.Spot);
			ordervo1.setPaidFull(true);
			ordervo1.setAddress("上海市闸北区灵石路636号");
			ordervo1.setLeaveWord("ymt autotest");
			ordervo1.setPhone("13100000001");
			ordervo1.setPostCode("200001");
			ordervo1.setTelephone("021-51002100");
			ordervo1.setReceiveName("李四");
			List<OrderItemVo> items = new ArrayList<OrderItemVo>();
			OrderItemVo orderitem1 = new OrderItemVo();
			orderitem1.setCatalogId(catalogId);
			orderitem1.setProductNum(1);
			orderitem1.setProductName("autotest补贴商品测试");
			orderitem1.setPriceType(PriceTypeEnum.VipPrice);
			orderitem1.setProductId(productId);

			// price = new BigDecimal(210);
			orderitem1.setEarnestPrice(productPrice);
			orderitem1.setProductPrice(productPrice);
			orderitem1.setOriginalPrice(productPrice);
			orderitem1.setSalesType(SalesTypeEnum.AlreadyInStock);
			orderitem1.setLogisticsType(LogisticsTypeEnum.SailProtect);
			orderitem1
					.setProductRefundChannel(ProductRefundChannelEnum.Official);
			orderitem1.setBrandId(5);
			orderitem1.setPackageNo("11111111");
			orderitem1
					.setProductDetailInfo("e5a1eccf-6a14-458f-b907-35dc9a3d2713#适用人群:b13e9eb1-98d6-4d39-8c35-225758e443d3#男");
			orderitem1.setProductRecordNo("1234567");
			orderitem1
					.setProductRefundChannel(ProductRefundChannelEnum.Official);
			orderitem1
					.setProperty("06f25e0c-e431-44ae-8a07-4d0bc824e3bd#尺寸:951a814e-2ded-4241-b9a0-91b2cd9a44fd#6,d2f663ea-3214-4344-abce-a8eaffcc43ce#颜色:f3ed3ad7-a084-450b-b698-256542963306#红色");
			ActivityVo activity = new ActivityVo();
			activity.setActivityId(activityId);
			activity.setActivityBeginDate(YMTDateUtil.getBeforeOrNextDay(-10,
					"yyyy-MM-dd'T'HH:mm:ss"));
			activity.setActivityEndDate(YMTDateUtil.getBeforeOrNextDay(10,
					"yyyy-MM-dd'T'HH:mm:ss"));
			activity.setActivityTemplateId(12345678);
			activity.setCommissionRate(new BigDecimal(1.1));
			activity.setCost(true);
			activity.setFeeFree(false);
			activity.setPremium(new BigDecimal(1.5));
			activity.setPromotionPrice(new BigDecimal(15));
			activity.setPromotionType(ActivityPromotionTypeEnum.DiscountRate);
			activity.setUseCoupon(true);
			orderitem1.setActivity(activity);
			items.add(orderitem1);
			ordervo1.setOrderItems(items);
			ordervo1.setFreight(new BigDecimal("21"));
			orders.add(ordervo1);
			groupVo1.setOrders(orders);
			placeorderBean.setOrderGroups(Arrays.asList(groupVo1));
			placeorderCall.setData(false, placeorderBean);
			placeorderCall.callService();

			int tradingId = placeorderCall.getTradingId();
			int orderId = placeorderCall.getOrderIds().getInt(0);
			map.put("orderId", orderId);
			map.put("tradingId", tradingId);
			map.put("sellerId", sellerId);
		} catch (Exception e) {
			Logger.fail(e);
		}
		return map;
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SettleSubsidy_001() {
		Logger.start(true, "补贴商品结算成功");
		try {
			OrderSettlementReq generateinvoicesonorderreceivedBean = new OrderSettlementReq();
			GenerateInvoicesOnOrderReceivedCall generateinvoicesonorderreceivedCall = new GenerateInvoicesOnOrderReceivedCall();
			// 创建订单
			Logger.comment("前置数据准备");
			Map map = placeOrder2();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int orderId = (Integer) map.get("orderId");
			int tradingId = (Integer) map.get("tradingId");
			int sellerId = (Integer) map.get("sellerId");
			OrderCallService orderCallService = new OrderCallService();
			// 应付款全金额
			BigDecimal totalPrice = orderCallService
					.getNeedPrePayAmountOfCash(orderId);
			// 支付订单
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);
			// 接单
			orderCallService.acceptOrder(orderId, sellerId);
			// 商家发货
			orderCallService.dispatched(orderId, sellerId);
			// 确认收货
			orderCallService.confirmReceive(userId, orderId);
			generateinvoicesonorderreceivedBean.setOrderId(orderId);
			generateinvoicesonorderreceivedCall.setData(false,
					generateinvoicesonorderreceivedBean);
			Logger.comment("生成订单结算,扣拥结算单");
			generateinvoicesonorderreceivedCall.callService();
			Logger.comment("前置数据准备结束");
			// 结算前账户金额
			DefaultWapper defaultWapper = new DefaultWapper();
			HashMap<String, Integer> m1 = new HashMap<String, Integer>();
			m1.put("UserId", sellerId);
			BigDecimal price1 = (BigDecimal) defaultWapper
					.selectAccountInfo(m1).get(0).get("fAvailAmount");
			settleSubsidyBean.setOrderId(orderId);
			settleSubsidyCall.setData(settleSubsidyBean);
			settleSubsidyCall.callService();
			Logger.verifyEquals("ok", settleSubsidyCall.getReturnData(),
					"验证返回报文");
			BigDecimal price2 = (BigDecimal) defaultWapper
					.selectAccountInfo(m1).get(0).get("fAvailAmount");
			// 补贴结算
			List<Map> subsidySettlementList = defaultWapper
					.selectSubsidySettlementInvoiceByOrderId(orderId);

			BigDecimal subsidyAmount = (BigDecimal) subsidySettlementList
					.get(0).get("SubsidyAmount");
			Logger.verifyEquals(subsidyAmount, price2.subtract(price1),
					"验证商家账务余额变化");
			Logger.verifyEquals(
					1,
					(Integer) subsidySettlementList.get(0).get(
							"SettlementStatus"), "验证SettlementStatus");
			Logger.verifyEquals("订单补贴", (String) subsidySettlementList.get(0)
					.get("Remark"), "验证Remark");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SettleSubsidy_002() {
		Logger.start(true, "补贴商品重复结算");
		try {
			OrderSettlementReq generateinvoicesonorderreceivedBean = new OrderSettlementReq();
			GenerateInvoicesOnOrderReceivedCall generateinvoicesonorderreceivedCall = new GenerateInvoicesOnOrderReceivedCall();
			// 创建订单
			Logger.comment("前置数据准备");
			Map map = placeOrder2();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int orderId = (Integer) map.get("orderId");
			int tradingId = (Integer) map.get("tradingId");
			int sellerId = (Integer) map.get("sellerId");
			OrderCallService orderCallService = new OrderCallService();
			// 应付款全金额
			BigDecimal totalPrice = orderCallService
					.getNeedPrePayAmountOfCash(orderId);
			// 支付订单
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);
			// 接单
			orderCallService.acceptOrder(orderId, sellerId);
			// 商家发货
			orderCallService.dispatched(orderId, sellerId);
			// 确认收货
			orderCallService.confirmReceive(userId, orderId);
			generateinvoicesonorderreceivedBean.setOrderId(orderId);
			generateinvoicesonorderreceivedCall.setData(false,
					generateinvoicesonorderreceivedBean);
			Logger.comment("生成订单结算,扣拥结算单");
			generateinvoicesonorderreceivedCall.callService();
			Logger.comment("前置数据准备结束");
			// 结算前账户金额
			DefaultWapper defaultWapper = new DefaultWapper();
			HashMap<String, Integer> m1 = new HashMap<String, Integer>();
			m1.put("UserId", sellerId);
			BigDecimal price1 = (BigDecimal) defaultWapper
					.selectAccountInfo(m1).get(0).get("fAvailAmount");
			settleSubsidyBean.setOrderId(orderId);
			settleSubsidyCall.setData(settleSubsidyBean);
			settleSubsidyCall.callService();
			Logger.comment("重复结算");
			settleSubsidyCall.callService();
			Logger.verifyEquals("ok", settleSubsidyCall.getReturnData(),
					"验证返回报文");
			BigDecimal price2 = (BigDecimal) defaultWapper
					.selectAccountInfo(m1).get(0).get("fAvailAmount");
			// 补贴结算
			List<Map> subsidySettlementList = defaultWapper
					.selectSubsidySettlementInvoiceByOrderId(orderId);

			BigDecimal subsidyAmount = (BigDecimal) subsidySettlementList
					.get(0).get("SubsidyAmount");
			Logger.verifyEquals(subsidyAmount, price2.subtract(price1),
					"验证商家账务余额变化");
			Logger.verifyEquals(
					1,
					(Integer) subsidySettlementList.get(0).get(
							"SettlementStatus"), "验证SettlementStatus");
			Logger.verifyEquals("订单补贴", (String) subsidySettlementList.get(0)
					.get("Remark"), "验证Remark");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SettleSubsidy_003() {
		Logger.start(false, "订单号为null");
		try {

			// settleSubsidyBean.setOrderId(orderId);
			settleSubsidyCall.setData(settleSubsidyBean);
			settleSubsidyCall.callService();
			Logger.comment("重复结算");
			settleSubsidyCall.callService();
			Logger.verifyEquals("ok", settleSubsidyCall.getReturnData(),
					"验证返回报文");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
